//
//  JJUserMessageCellAdapter.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/1.
//

import UIKit


let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height



struct MsgCellUserInfoLayoutAdapter {
    var headTop : CGFloat = 4
    var headFromSide : CGFloat =  14
    var headWidth : CGFloat =  40
    var headHeight : CGFloat =  40
    var nameTop : CGFloat = 4
    var nameFromSide : CGFloat = 4
    
    //static //name部分的占位高度(不含距上,计算时需要加上)
    var nameHeight : CGFloat = 14 //  //建议 : 字号值 + 2
    
    //userinfo 头像展示时占用宽度
    func headTakeWidth() -> CGFloat {
        return headFromSide + headWidth
    }
    
    //userinfo 名字展示时占用高度
    func nameTakeHeight() -> CGFloat {
        return nameTop + nameHeight
    }
}

struct MsgCellMsgBackgroundLayoutAdapter {
    
    var top : CGFloat = 4
    var fromSide : CGFloat = 4
    var toSide : CGFloat = 80
    var bottom : CGFloat = 8

    
    //计算message背景宽度 (这个不是最大宽度,这里不做自动撑宽,而是实际宽度)
    //这里不再做气泡,因为涉及到有些内容将不会在气泡内,所以气泡的范围并不能确定.
    //messageBackground宽度
    func msgBackgroundWidth(userInfoAdapter : MsgCellUserInfoLayoutAdapter) -> CGFloat {
        //获取宽度时必定会算入带头像时的宽度
        return SCREEN_WIDTH - userInfoAdapter.headTakeWidth() - self.fromSide - self.toSide;

    }
    
    //计算 cell的除去背景的高度  cell_H - msgbackground_H
    func cellHeight_subtractMsgBackgroundHeight(userInfoAdapter : MsgCellUserInfoLayoutAdapter , showName : Bool) -> CGFloat {
        return self.top + self.bottom + (showName ? userInfoAdapter.nameTakeHeight() : 0);
    }
    //计算给出最小高度(如果做好限定,这个高度其实可以放弃)
    func cellMinHeight(userInfoAdapter : MsgCellUserInfoLayoutAdapter) -> CGFloat {
        return userInfoAdapter.headTop + userInfoAdapter.headHeight + self.bottom
            
        
    }
}

struct MsgCellMsgContentLayoutAdapter {
    var top : CGFloat = 10
    var bottom : CGFloat = 10
    var fromSide : CGFloat = 10
    var toSide : CGFloat = 10
    
//    //刨去内容的高度 (含内容上下间距,而不含内容本身) 默认不隐藏名字
//    func fetchHeight_beyondContent() -> Float{
//        return JJMessageCellAdapterInfo.backgroundAdapter.beyondMessageBackgroundHeight() + top + bottom
//    }
    
    //刨去内容的高度 (含内容上下间距,而不含内容本身)
    func cellHeight_subtractMsgContentHeight(userInfoAdapter : MsgCellUserInfoLayoutAdapter ,backgroundAdapter : MsgCellMsgBackgroundLayoutAdapter ,showName : Bool) -> CGFloat{
        backgroundAdapter.cellHeight_subtractMsgBackgroundHeight(userInfoAdapter: userInfoAdapter, showName: showName) + self.top + self.bottom
    }
    
    //最大宽度.
    func msgContentMaxWidth(userInfoAdapter : MsgCellUserInfoLayoutAdapter ,backgroundAdapter : MsgCellMsgBackgroundLayoutAdapter) -> CGFloat{
        backgroundAdapter.msgBackgroundWidth(userInfoAdapter: userInfoAdapter) - self.fromSide - self.toSide
    }
    
}


struct MsgCellBubbleLayoutAdapter {
    
    var sendImageName = "loginFail"
    var receiveImageName = "loginFail"

    //将会有多种气泡,每一种气泡的内边缘将不尽相同.
    var top : CGFloat = 10
    var bottom : CGFloat = 10
    var fromSide : CGFloat = 10
    var toSide : CGFloat = 10
    
    //上左下右的免拉伸距离
    var resizableTop : CGFloat = 10
    var resizableBottom : CGFloat = 10
    var resizableFromSide : CGFloat = 10
    var resizableToSide : CGFloat = 10
    
    func bubbleImage(direction:EMMessageDirection) -> UIImage{
        if direction == .send {
            return UIImage.init(named: sendImageName)!.resizableImage(withCapInsets:UIEdgeInsets(top: resizableTop, left: resizableToSide, bottom: resizableBottom, right:resizableFromSide))
        }else if direction == .receive {
            return UIImage.init(named: receiveImageName)!.resizableImage(withCapInsets:UIEdgeInsets(top: resizableTop, left: resizableFromSide, bottom: resizableBottom, right: resizableToSide))
        }else{
            return UIImage.init(named: receiveImageName)!
        }
    }
}
